package org.opnlp.opnlp2.parser;

import java.util.LinkedList;

public class HTMLCleanerEngine {

    /**
     * Method that cleans HTML tags and quote marks from text
     * @param originalText to be cleaned
     * @return string that represents cleaned text
     * @author Stefan
     * @version 1.1
     */
    public static String clean(String originalText){
		
	String text = originalText;
	text = text.replaceAll("<", " <");
	text = text.replaceAll(">", "> ");
        text = text.replaceAll("\\\\", "");
        text = text.replaceAll("\"", "");
		
	String[] textArray = text.split(" ");
		
	boolean clean=false;
	int begIndex=0;
	int endIndex;
			
        for (int i=0;i<textArray.length;i++){
            if(textArray[i].contains("<")){
		clean=true;
		begIndex=i;
            }
			
            if(textArray[i].contains(">") && clean){
		endIndex=i;		
		for (int j=begIndex;j<=endIndex;j++){
                    textArray[j]="";			
                }
		clean=false;
            }	
        }
		
    text="";
    for (String string : textArray) {
        text=text+" "+string;
    }	
    return text;
}

    /**
     * Another method for cleaning text from HTML tags. Not needed at the moment
     * @param originalText to be cleaned
     * @return cleaned text
     * @author Stefan
     * @version 1.0
     */
    public String clean2(String originalText){
			
		String string = originalText;
		LinkedList<Character> chars = new LinkedList<Character>();
		
		for (int i = 0; i < string.length(); i++)
			chars.add(string.charAt(i));
		
		boolean clean=false;
		int begIndex=0;
		int endIndex;
		
		for (int i = 0; i < chars.size(); i++){
			
			if(chars.get(i).equals('<')){
				begIndex=i;
				clean=true;
			}
			
			if(!clean) System.out.println("Stay: " + chars.get(i));
			
			if(chars.get(i).equals('>') && clean){
				endIndex=i;
				
				System.out.println("remove: " + chars.get(i));
				
				for(int j=begIndex;j<=endIndex;j++){
					System.out.println("remove: " + chars.get(j));
					chars.remove(j);
				}
				clean=false;	
			}
		}
		String text="";
		for (Character character : chars) {
			text=text+character;
		}
		return text;
	}
}
	


